<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_023sm/model')->方法名();
*/
namespace jy_023sm;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	//发起http请求
	public function request($rurl,$post=null,$isres=false){
		$post=ijson_encode($post);
		$result=cfc('http')->ihttp_request($rurl,$post);
		if(is_error($result)){			
			$message="访问失败, 错误: {$result['message']}";
			if($return_error){
				return error($message);
			}else{
				exi($message,'error');
			}
		}
		$data=preg_replace('/[\x00-\x1F]/','',$result['content']);
		$data=substr($data,0,-64);
		$data=@json_decode($data,true);
		//print_r($data);exit;
		if(!is_array($data) || !$data){
			$message='访问接口错误，以下为原始数据：'.$result['content'];
			if($return_error){
				return error($message);
			}else{
				exi($message,'error');
			}
		}
		return $data;
	}
	public function uplog($content,$type=0,$isexi=false) {
		$basic=$this->plugin_setting('basic');
		if($basic['isnodemo']){
			$url = "https://zycp.023sm.com:8092/receive/tracedata";
		}else{
			$url = "https://zycp.023sm.com:8092/receive/tracedata";
		}
		if(!$basic['AUTH_ID'] || !$basic['CITY_CODE']|| !$basic['SOCIAL_CRE']|| !$basic['COMP_NAME']){
			exi('接口认证编码,市行政区划代码,法人责任主体代码,法人责任主体名称必填','error');
		}
		$post=array(
			'BIZ_TYPE'=>'cityTraceRequest',
			'REQ_TIME'=>date('Y-m-d H:i:s'),
			'REQ_ID'=>get_ordersn('jy_023sm_log','logsn'),
			'AUTH_ID'=>$basic['AUTH_ID'],
			'PARAM'=>array(
				'TABLE_NAME'=>'',
				'DATA'=>array(),
			)
		);
		if($type==1){
			$post['PARAM']['TABLE_NAME']='TRACE_BASE_NODE';
			$DATA=array(
				'CITY_CODE'=>$basic['CITY_CODE'],
				'AREA_CODE'=>$basic['AREA_CODE']?:'',
				'SOCIAL_CRE'=>$basic['SOCIAL_CRE'],
				'COMP_NAME'=>$basic['COMP_NAME'],
				'LEGAL_TYPE'=>$basic['LEGAL_TYPE'],
				'LEGAL_ID'=>$basic['LEGAL_ID']?:'',
				'LEGAL_PERSON'=>$basic['LEGAL_PERSON'],
				'RECORD_DATE'=>$basic['RECORD_DATE'],
				'ISSUING_TIME'=>$basic['ISSUING_TIME']?:'',
				'TERM_OF_VALIDITY'=>$basic['TERM_OF_VALIDITY']?:'0000-00-00',
				'RECORD_NODE_ID'=>$basic['RECORD_NODE_ID'],
				'RECORD_NODE_NAME'=>$basic['RECORD_NODE_NAME'],
				'BOOTH_NO'=>$basic['BOOTH_NO'],
				'ADDRESS'=>$basic['ADDRESS'],
				'LONGITUDE'=>$basic['LONGITUDE'],
				'CONTACTS'=>$basic['CONTACTS'],
				'TEL'=>$basic['TEL'],
				'FAX'=>$basic['FAX'],
				'NODE_TYPE'=>$basic['NODE_TYPE'],
				'CLASS_TYPE'=>$basic['CLASS_TYPE'],
				'INDUSTRY_CODE'=>$basic['INDUSTRY_CODE'],
				'REG_TYPE'=>$basic['REG_TYPE'],
			);
			if($content['UPDATE_TIME']){
				$DATA['UPDATE_TIME']=$content['UPDATE_TIME'];
			}
			$post['PARAM']['DATA']=$DATA;
		}elseif($type==2){
			exi('暂时不支持自然人备案！','error');
		}elseif($type==3){
			$post['BIZ_TYPE']='cityTraceResponse';
			$post['PARAM']['TABLE_NAME']='TRACE_SCAN';
		}elseif(!$type){
			$islog1=pdo_get('jy_023sm_log',array('uniacid'=>$_SESSION['uniacid'],'type'=>1));
			if($islog1['status']!=1){
				exi('请先《法人备案》成功，才能上报商品！','error');
			}
			if(!$basic['BUSINESS_CODE'] || !$basic['BUSINESS_NAME']|| !$basic['TRACE_VARI']|| !$basic['CLASS_TYPE']|| !$content['goodstitle']){
				exi('主要大类代码,经营户编码,经营户名称,追溯品种代码，商品名称必填','error');
			}
			$post['PARAM']['TABLE_NAME']='TRACE_VARIETY_TYPE';
			$DATA=array(
				'CITY_CODE'=>$basic['CITY_CODE'],
				'AREA_CODE'=>$basic['AREA_CODE']?:'',
				'SOCIAL_CRE'=>$basic['SOCIAL_CRE'],
				'COMP_NAME'=>$basic['COMP_NAME'],
				'BUSINESS_CODE'=>$basic['BUSINESS_CODE'],
				'BUSINESS_NAME'=>$basic['BUSINESS_NAME'],
				'TRACE_VARI'=>$basic['TRACE_VARI'],
				'MAIN_TYPE'=>1,
				'CLASS_TYPE'=>$basic['CLASS_TYPE'],
				'COMMONDITY'=>$content['goodstitle'],
			);
			if($content['UPDATE_TIME']){
				$DATA['UPDATE_TIME']=$content['UPDATE_TIME'];
			}
			$post['PARAM']['DATA']=$DATA;
		}
		$res=$this->request($url,$post,$isres);
		$result=$res['RESULT'];
		//print_r($result);exit;
		if(!$result['SUCCESS']){
			$status=0;
		}else{
			$status=1;
		}
		$log=array(
			'uniacid'=>$_SESSION['uniacid'],
			'goodsid'=>$content['goodsid'],
			'pluginname'=>$content['pluginname'],
			'logsn'=>$post['REQ_ID'],
			'type'=>$type,
			'content'=>ijson_encode($post),
			'result'=>ijson_encode($result),
			'status'=>$status,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_023sm_log',$log);
		if(!$result['SUCCESS'] || $result['SUCCESS']!='true'){
			if($isexi){
				exi('获取失败：'.$result['RESULT_CODE'].','.$result['RESULT_MSG'],'error');
			}else{
				return $result['RESULT_CODE'].','.$result['RESULT_MSG'];
			}
		}
		return true;
	}
}
?>